package com.acwing.partition11;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/12/23 10:22
 */
public class AC1074二叉苹果树 {

    private static final int N = 110, M = N * 2;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[][] f = new int[N][N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); m = nextInt();
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            int a = nextInt(), b = nextInt(), c = nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        dfs(1, -1);
        writer.println(f[1][m]);
        writer.flush();
    }

    private static void dfs(int u, int fa) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == fa) continue;
            dfs(v, u);
            for (int j = m; j >= 0; j--) {
                for (int k = 0; k < j; k++) {
                    f[u][j] = Math.max(f[u][j], f[u][j - k - 1] + f[v][k] + w[i]);
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
